﻿
#include "MeasureView/ToolBarMeasure.h"
#include "MeasureView/MeasureView.h"
#include <QAction>
#include <QActionGroup>
#include <QMenu>
#include <QIcon>

namespace Qly
{

ToolBarMeasure::ToolBarMeasure(QWidget *parent)
    :QToolBar(parent)
{
    setIconSize(QSize(32, 32));
    setObjectName(QStringLiteral("ToolBarMeasure"));
    initAction();
    pMeasure = nullptr;
}

ToolBarMeasure::ToolBarMeasure(const QString &title, QWidget *parent)
    :QToolBar(title, parent)
{
    setIconSize(QSize(32, 32));
    setObjectName(QStringLiteral("ToolBarMeasure"));
    initAction();
    pMeasure = nullptr;
}

void ToolBarMeasure::attach(MeasureView *widget)
{
    if(pMeasure != widget && pMeasure != nullptr)
    {
        pMeasure->dataSet().setElementType(MeasureElement::None);
        uncheckAll();
        disconnect(actionShowDialogMeasure, &QAction::triggered, &pMeasure->dataSet(), &Qly::MeasureDataSet::showMeasureDialog);
    }
    pMeasure = widget;
    if(pMeasure != nullptr)
    {
        pMeasure->dataSet().setElementType(MeasureElement::None);
        uncheckAll();
        connect(actionShowDialogMeasure, &QAction::triggered, &widget->dataSet(), &Qly::MeasureDataSet::showMeasureDialog);
    }
}

void ToolBarMeasure::attach(QMenu *pMenu)
{
    pMenu->addAction(actionSelect);
    pMenu->addAction(actionDelete);
    pMenu->addAction(actionLine);
    pMenu->addAction(actionParallel);
    pMenu->addAction(actionDots2Line);
    pMenu->addAction(actionPolyline);

    pMenu->addAction(actionRect);
    pMenu->addAction(actionPolygon);

    pMenu->addAction(actionAngle);
    pMenu->addAction(actionAngle2);

    pMenu->addAction(actionLCircle);
    pMenu->addAction(actionPCircle);
    pMenu->addAction(actionRCircle);
    pMenu->addAction(actionCircle2Circle);
    pMenu->addAction(actionCount);

    pMenu->addSeparator();
    pMenu->addAction(actionShowDialogMeasure);
}

void ToolBarMeasure::initAction()
{
    setWindowTitle("Measurement Toolbar");
    setObjectName(QStringLiteral("ToolBarMeasure"));

    actionSelect = new QAction(tr("select"), this);
    actionSelect->setObjectName(QStringLiteral("actionSelect"));
    actionSelect->setCheckable(true);
    QIcon iconSelect;
    iconSelect.addFile(QStringLiteral(":/Measurement/select.png"), QSize(), QIcon::Normal, QIcon::Off);
    //iconSelect.addFile(QStringLiteral(":/new/prefix1/res/select.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionSelect->setIcon(iconSelect);


    actionDelete = new QAction(tr("delete"), this);
    actionDelete->setObjectName(QStringLiteral("actionDelete"));
    actionDelete->setCheckable(true);
    QIcon iconDelete;
    iconDelete.addFile(QStringLiteral(":/Measurement/delete.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionDelete->setIcon(iconDelete);

    actionLine = new QAction(tr("pt to pt dist"), this);
    actionLine->setObjectName(QStringLiteral("actionLine"));
    actionLine->setCheckable(true);
    QIcon iconLine;
    iconLine.addFile(QStringLiteral(":/Measurement/line.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLine->setIcon(iconLine);

    actionParallel = new QAction(tr("line to line dist"), this);
    actionParallel->setObjectName(QStringLiteral("actionParallel"));
    actionParallel->setCheckable(true);
    QIcon iconParallel;
    iconParallel.addFile(QStringLiteral(":/Measurement/Parallel.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionParallel->setIcon(iconParallel);

    actionPolyline = new QAction(tr("poly lines"), this);
    actionPolyline->setObjectName(QStringLiteral("actionPolyline"));
    actionPolyline->setCheckable(true);
    QIcon icon14;
    icon14.addFile(QStringLiteral(":/Measurement/polyline.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionPolyline->setIcon(icon14);

    actionRCircle = new QAction(tr("radius circle"), this);
    actionRCircle->setObjectName(QStringLiteral("actionRCircle"));
    actionRCircle->setCheckable(true);
    QIcon icon3;
    icon3.addFile(QStringLiteral(":/Measurement/RCircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionRCircle->setIcon(icon3);

    actionLCircle = new QAction(tr("diameter circle"), this);
    actionLCircle->setObjectName(QStringLiteral("actionLCircle"));
    actionLCircle->setCheckable(true);
    QIcon icon4;
    icon4.addFile(QStringLiteral(":/Measurement/LCircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionLCircle->setIcon(icon4);

    actionPCircle = new QAction(tr("tripoint circle"), this);
    actionPCircle->setObjectName(QStringLiteral("actionPCircle"));
    actionPCircle->setCheckable(true);
    QIcon icon16;
    icon16.addFile(QStringLiteral(":/Measurement/pcircle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionPCircle->setIcon(icon16);

    actionPolygon = new QAction(tr("polygon area"), this);
    actionPolygon->setObjectName(QStringLiteral("actionPolygon"));
    actionPolygon->setCheckable(true);
    QIcon icon15;
    icon15.addFile(QStringLiteral(":/Measurement/polygon.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionPolygon->setIcon(icon15);

    actionRect = new QAction(tr("rect area"), this);
    actionRect->setObjectName(QStringLiteral("actionRect"));
    actionRect->setCheckable(true);
    QIcon icon5;
    icon5.addFile(QStringLiteral(":/Measurement/rect.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionRect->setIcon(icon5);

    actionCircle2Circle = new QAction(tr("center to center dist"), this);
    actionCircle2Circle->setObjectName(QStringLiteral("actionCircle2Circle"));
    actionCircle2Circle->setCheckable(true);
    icon5.addFile(QStringLiteral(":/Measurement/Circle2Circle.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionCircle2Circle->setIcon(icon5);
    actionAngle = new QAction(tr("angle"), this);
    actionAngle->setObjectName(QStringLiteral("actionCorner"));
    actionAngle->setCheckable(true);
    QIcon icon6;
    icon6.addFile(QStringLiteral(":/Measurement/corner.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionAngle->setIcon(icon6);


    actionAngle2 = new QAction(tr("line to line angle"), this);
    actionAngle2->setObjectName(QStringLiteral("actionCorner2"));
    actionAngle2->setCheckable(true);
    QIcon iconAutoParallel;
    iconAutoParallel.addFile(QStringLiteral(":/Measurement/angle2.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionAngle2->setIcon(iconAutoParallel);

    actionArc = new QAction(tr("angle of arc"), this);
    actionArc->setObjectName(QStringLiteral("actionCorner"));
    actionArc->setCheckable(true);
    QIcon icon7;
    icon7.addFile(QStringLiteral(":/Measurement/arc.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionArc->setIcon(icon7);
    actionDots2Line = new QAction(tr("point to line distance"), this);
    actionDots2Line->setObjectName(QStringLiteral("actionDots2Line"));
    actionDots2Line->setCheckable(true);
    QIcon icon17;
    icon17.addFile(QStringLiteral(":/Measurement/DotsToLine.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionDots2Line->setIcon(icon17);


    actionCount = new QAction(tr("Count"), this);
    actionCount->setObjectName(QStringLiteral("actionCount"));
    actionCount->setCheckable(true);
    QIcon iconCount;
    iconCount.addFile(QStringLiteral(":/Measurement/Count.png"), QSize(), QIcon::Normal, QIcon::Off);
    actionCount->setIcon(iconCount);

    actionShowDialogMeasure = new QAction(tr("Show Measure Dialog"));


    addAction(actionSelect);
    addAction(actionDelete);
    addAction(actionLine);
    addAction(actionParallel);
    addAction(actionPolyline);
    addAction(actionRCircle);
    addAction(actionLCircle);
    addAction(actionPCircle);
    addAction(actionRect);
    addAction(actionPolygon);
    addAction(actionCircle2Circle);
    addAction(actionAngle);
    addAction(actionAngle2);
    addAction(actionArc);
    addAction(actionDots2Line);
    addAction(actionCount);

    actionGroup = new QActionGroup(this);
    actionGroup->addAction(actionSelect);
    actionGroup->addAction(actionDelete);
    actionGroup->addAction(actionLine);
    actionGroup->addAction(actionParallel);
    actionGroup->addAction(actionPolyline);
    actionGroup->addAction(actionRCircle);
    actionGroup->addAction(actionLCircle);
    actionGroup->addAction(actionPCircle);
    actionGroup->addAction(actionRect);
    actionGroup->addAction(actionPolygon);
    actionGroup->addAction(actionCircle2Circle);
    actionGroup->addAction(actionAngle);
    actionGroup->addAction(actionAngle2);
    actionGroup->addAction(actionArc);
    actionGroup->addAction(actionDots2Line);
    actionGroup->addAction(actionCount);

    connect(actionSelect, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionDelete, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));

    connect(actionLine, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));

    connect(actionParallel, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionLCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionRCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionRect, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionCircle2Circle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionAngle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionAngle2, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionArc, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionPolygon, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionPolyline, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionPCircle, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionDots2Line, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
    connect(actionCount, SIGNAL(triggered(bool)), this, SLOT(upateDrawType(bool)));
}

void ToolBarMeasure::upateDrawType(bool on)
{
    Q_UNUSED(on);

    if(pMeasure == nullptr) return;
    Qly::MeasureDataSet & data = pMeasure->dataSet();
    QAction * action = (QAction *) sender();
//    uncheckAll();

//    action->setChecked(true);
    if( action == actionSelect )
    {
        data.setElementType(Qly::MeasureElement::None);
    }

    else if( action == actionDelete )
    {
        data.setElementType(Qly::MeasureElement::Delete);
    }
    else if(action == actionLine)
    {
        data.setElementType(Qly::MeasureElement::Line);
    }

    else if(action == actionParallel)
    {
        data.setElementType(Qly::MeasureElement::ParallelLine);
    }
    else if(action == actionLCircle)
    {
        data.setElementType(Qly::MeasureElement::Circle2P);
    }
    else if(action == actionRCircle)
    {
        data.setElementType(Qly::MeasureElement::CircleCenter);
    }
    else if(action == actionRect)
    {
        data.setElementType(Qly::MeasureElement::Rect);
    }
    else if(action == actionCircle2Circle)
    {
        data.setElementType(Qly::MeasureElement::Circle2Circle);
 //       centerWidget->setDrawType(Element::CIRCLE2CIRCLE);
    }
    else if(action == actionAngle)
    {
        data.setElementType(Qly::MeasureElement::Angle3P);
    }
    else if(action == actionAngle2)
    {
        data.setElementType(Qly::MeasureElement::Angle4P);
    }
    else if(action == actionArc)
    {
        data.setElementType(Qly::MeasureElement::Arc3P);
    }
    else if(action == actionPolyline)
    {
        data.setElementType(Qly::MeasureElement::Polyline);
    }
    else if(action == actionPolygon)
    {
        data.setElementType(Qly::MeasureElement::Polygon);
    }
    else if(action == actionPCircle)
    {
        data.setElementType(Qly::MeasureElement::Circle3P);
    }
    else if(action == actionDots2Line)
    {
        data.setElementType(Qly::MeasureElement::Dots2Line);
    }
    else if(action == actionCount)
    {
        data.setElementType(Qly::MeasureElement::Count);
    }
    emit checked();
}

void ToolBarMeasure::uncheckAll()
{
    actionSelect->setChecked(false);
    actionDelete->setChecked(false);
    actionLine->setChecked(false);
    actionAngle2->setChecked(false);
    actionParallel->setChecked(false);
    actionLCircle->setChecked(false);
    actionRCircle->setChecked(false);
    actionRect->setChecked(false);
    actionAngle->setChecked(false);
    actionArc->setChecked(false);
    actionCircle2Circle->setChecked(false);
    actionPolygon->setChecked(false);
    actionPolyline->setChecked(false);
    actionPCircle->setChecked(false);
    actionDots2Line->setChecked(false);
}

} //namespace Qly
